package com.platform.common.cache.redis;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.databind.JavaType;
import com.platform.common.cache.redis.serializer.GenericMsgPackRedisSerializer;
import com.platform.common.cache.redis.serializer.MsgPackRedisSerializer;

/**
 * RedisTemplate构建工具类，使用指定的序列化：msgpack或JacksonJson<br/>
 * Note: 不要使用默认的低效不安全的JDK序列化
 */
public class RedisTemplateBuildUtils {

	public static final GenericMsgPackRedisSerializer MSG_PACK_SER = new GenericMsgPackRedisSerializer();
	public static final RedisSerializer<String> STRING_SER = new StringRedisSerializer();

	private RedisTemplateBuildUtils() {
	}

	/**
	 * 构建key,hashKey使用字符串序列化，value使用msgpack一般序列化的RedisTemplate
	 */
	public static RedisTemplate<String, ?> buildGenericMsgPackRedisTemplate(RedisConnectionFactory connectionFactory) {
		RedisTemplate<String, ?> redisTpl = new RedisTemplate<>();
		redisTpl.setConnectionFactory(connectionFactory);
		redisTpl.setDefaultSerializer(MSG_PACK_SER);
		redisTpl.setKeySerializer(STRING_SER);
		redisTpl.setHashKeySerializer(STRING_SER);
		redisTpl.setValueSerializer(MSG_PACK_SER);
		redisTpl.afterPropertiesSet();
		return redisTpl;
	}

	/**
	 * 构建key,hashKey使用字符串序列化，value使用msgpack具体类型序列化的RedisTemplate
	 */
	public static <T> RedisTemplate<String, T> buildTypedMsgPackRedisTemplate(RedisConnectionFactory connectionFactory, Class<T> type) {
		MsgPackRedisSerializer msgPackSer = new MsgPackRedisSerializer(type);
		RedisTemplate<String, T> redisTpl = new RedisTemplate<>();
		redisTpl.setConnectionFactory(connectionFactory);
		redisTpl.setDefaultSerializer(msgPackSer);
		redisTpl.setKeySerializer(STRING_SER);
		redisTpl.setHashKeySerializer(STRING_SER);
		redisTpl.setValueSerializer(msgPackSer);
		redisTpl.afterPropertiesSet();
		return redisTpl;
	}

	/**
	 * 构建key,hashKey使用字符串序列化，value使用JacksonJson具体类型序列化的RedisTemplate
	 */
	public static <T> RedisTemplate<String, T> buildJacksonJsonRedisTemplate(RedisConnectionFactory connectionFactory, Class<T> type) {
		Jackson2JsonRedisSerializer<T> jsonSer = new Jackson2JsonRedisSerializer<>(type);
		RedisTemplate<String, T> redisTpl = new RedisTemplate<>();
		redisTpl.setConnectionFactory(connectionFactory);
		redisTpl.setDefaultSerializer(jsonSer);
		redisTpl.setKeySerializer(STRING_SER);
		redisTpl.setHashKeySerializer(STRING_SER);
		redisTpl.setValueSerializer(jsonSer);
		redisTpl.afterPropertiesSet();
		return redisTpl;
	}

	/**
	 * 构建key,hashKey使用字符串序列化，value使用JacksonJson具体类型序列化的RedisTemplate
	 */
	public static <T> RedisTemplate<String, T> buildJacksonJsonRedisTemplate(RedisConnectionFactory connectionFactory, JavaType javaType) {
		Jackson2JsonRedisSerializer<T> jsonSer = new Jackson2JsonRedisSerializer<>(javaType);
		RedisTemplate<String, T> redisTpl = new RedisTemplate<>();
		redisTpl.setConnectionFactory(connectionFactory);
		redisTpl.setDefaultSerializer(jsonSer);
		redisTpl.setKeySerializer(STRING_SER);
		redisTpl.setHashKeySerializer(STRING_SER);
		redisTpl.setValueSerializer(jsonSer);
		redisTpl.afterPropertiesSet();
		return redisTpl;
	}
	
//	
//	<!-- https://mvnrepository.com/artifact/org.msgpack/jackson-dataformat-msgpack -->
//	<dependency>
//		<groupId>org.msgpack</groupId>
//		<artifactId>jackson-dataformat-msgpack</artifactId>
//		<version>0.8.16</version>
//	</dependency>
}
